<template>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
      <!-- Content Header (Page header) -->
      <section class="content-header">
        <h1>
          客户管理
        </h1>

        <ol class="breadcrumb">
          <li><a href="#"> 控制台</a></li>
          <li><a href="#">联系人信息</a></li>
          <li class="active">编辑</li>
        </ol>
      </section>

      <!-- Main content -->
      <section class="content">
          <div class="row">
            <div class="col-xs-12">
                <div class="box box-solid">
                  <div class="box-header with-border">
                    <h3 class="box-title">编辑联系人信息</h3>
                  </div>
                  <!-- /.box-header -->
                  <!-- form start -->
                  <form class="form-horizontal">
                    <div class="box-body">
                        <div class="row">
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('custom')}]">
                              <label class="col-sm-2 control-label">公司</label>
                              <div class="col-sm-10">
                                <select class="form-control" v-model="model.custom_id" name="custom" v-validate="'required'">
                                  <option v-for="custom in custom_list" :value="custom.id">{{custom.name}}</option>
                                </select>
                                <span class="help-block" v-if="errors.has('custom')">{{errors.first('custom')}}</span>
                              </div>
                          </div>
                        </div>
                        <div class="row">
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('name')}]">
                              <label class="col-sm-2 control-label">名称</label>
                              <div class="col-sm-10">
                                <input name="name" class="form-control" placeholder="10个字" v-model="model.name" v-validate="'required|max:10'">
                                <span class="help-block" v-if="errors.has('name')">{{errors.first('name')}}</span>
                              </div>
                            </div>
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('gender')}]">
                                <label class="col-sm-2 control-label">性别</label>
                                <div class="col-sm-10">
                                  <select name="gender" class="form-control" v-model="model.gender" v-validate="'required|in:0,1'">
                                      <option value="1">男</option>
                                      <option value="0">女</option>
                                  </select>
                                  <span class="help-block" v-if="errors.has('gender')">{{errors.first('gender')}}</span>
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('office_phone')}]">
                                <label class="col-sm-2 control-label">办公电话</label>
                                <div class="col-sm-10">
                                    <input name="office_phone" class="form-control" placeholder="" v-model="model.office_phone">
                                    <span class="help-block" v-if="errors.has('office_phone')">{{errors.first('office_phone')}}</span>
                                </div>
                            </div>
                            <div :class="['form-group','col-sm-6',{'has-error':errors.has('mobile')}]">
                                <label  class="col-sm-2 control-label">手机</label>
                                <div class="col-sm-10">
                                    <input name="mobile" class="form-control" placeholder="" v-model="model.mobile" v-validate="'required|numeric|max:11'">
                                    <span class="help-block" v-if="errors.has('mobile')">{{errors.first('mobile')}}</span>
                                </div>
                            </div>
                        </div>
                      <div class="row">
                          <div :class="['form-group','col-sm-6',{'has-error':errors.has('title')}]">
                              <label class="col-sm-2 control-label">职位</label>
                              <div class="col-sm-10">
                                <input name="title" class="form-control" placeholder="20字以内" v-model="model.title" v-validate="'required|max:20'">
                                <span class="help-block" v-if="errors.has('title')">{{errors.first('title')}}</span>
                              </div>
                          </div>
                          <div :class="['form-group','col-sm-6',{'has-error':errors.has('remark')}]">
                              <label class="col-sm-2 control-label">备注</label>
                              <div class="col-sm-10">
                                <input name="remark" class="form-control" placeholder="150字以内" v-model="model.remark" v-validate="'max:150'">
                                <span class="help-block" v-if="errors.has('remark')">{{errors.first('remark')}}</span>
                              </div>
                          </div>
                      </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                      <button type="button" class="btn btn-primary" @click="submit">保存</button>
                    </div>
                    <!-- /.box-footer -->
                  </form>
                </div>
              <!-- /.box -->
            </div>
          </div>
      </section>
      <!-- /.content -->
    </div>
    <!-- /.content-wrapper -->
</template>

<script>
import Custom from '@/modules/Custom'
import Contact from '@/modules/Contact'

export default {

    mounted() {
        this.loadModel();
    },

    data() {
        return {
            model:{},

            //客户等级列表
            custom_list:[],
        };
    },

    methods: {
        loadModel() {
            if(this.$route.params.id > 0){
                Contact.show(this.$route.params.id)
                .then((response) => {
                    this.model = response.contents;
                });
            }
            Custom.list({status:0},false)
            .then((response) => {
                this.custom_list = response.contents;
            });
        },

        submit() {
            this.$validator.validateAll().then((result) => {
                if(!result){
                    return;
                }
                if(this.$route.params.id > 0){
                    Contact.put(this.$route.params.id,this.model)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'ContactIndex'});
                    })
                }else{
                    Contact.store(this.model)
                    .then((response) => {
                        $.message('操作成功');
                        this.$router.push({name:'ContactIndex'});
                    });
                }
            });
        }
    },

    computed:{

    }
}
</script>

<style lang="css">
</style>
